#import <Foundation/Foundation.h>
#import "../OmronFramework/OmronUSB.h"

void dumpNSData(NSMutableData *data, const int dataSize, const uint32_t baseAddress, const char *fileName)
{
    FILE *fHandle = NULL;
    
    fHandle = fopen(fileName, "a");
    if(fHandle == NULL)
    {
        NSLog(@"Error opening file");
        return;
    }
    
    // Log to disk, binary format
    fwrite([data mutableBytes], dataSize, 1, fHandle);
    
    fclose(fHandle);    
}


int main (int argc, const char * argv[]) 
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    NSMutableData *data = NULL;
    BOOL          result = FALSE;

    // insert code here...
    NSLog(@"Hello, World!");
    
    OmronUSB *pedometer = [OmronUSB new];

    // Turn off log messages
    [pedometer debugOn];
    
    result = [pedometer detect];

    if(result == NO)
    {
        NSLog(@"Pedometer not found");
        return(1);
    }
    
    for(int idx = 0; idx < 128; idx++)
    {
        printf("\nIndex %d\n", idx);
        
        data = [pedometer getReport:0];
        
        if(data == NULL)
        {
            NSLog(@"getReport failed!");
            return(2);
        }
        
        dumpNSData(data, 64*8, 8* idx * 64, "omronData");
        
        // Dump report
        [pedometer dumpBuffer];
    }
    
    [pool drain];
    return 0;
}
